package com.cloudbroker.bcs.csvc.credits.bizservice.impl;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.model.Pagination;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.credits.api.service.CreditsBizService;
import com.cloudbroker.bcs.csvc.credits.api.vo.back.*;
import com.cloudbroker.bcs.csvc.credits.api.vo.request.*;
import com.cloudbroker.bcs.csvc.credits.entity.Credits;
import com.cloudbroker.bcs.csvc.credits.entity.CreditsLog;
import com.cloudbroker.bcs.csvc.credits.service.CreditsLimitService;
import com.cloudbroker.bcs.csvc.credits.service.CreditsLogService;
import com.cloudbroker.bcs.csvc.credits.service.CreditsService;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lili19289 on 2016/11/7.
 */
@Service
public class CreditsBizServiceImpl implements CreditsBizService {


    @Resource
    private CreditsService creditsService;

    @Resource
    private CreditsLogService creditsLogService;


    @Resource
    private CreditsLimitService creditsLimitService;

    @Transactional
    public CreditsVO functionAddCredits(CreditsOperForm form) {
        Credits creditsParam = JSON.parseObject(JSON.toJSONString(form),Credits.class);
        Credits credits = creditsService.getByUser(creditsParam);
        int creditsNum=form.getOpera_credits();
        if(credits !=null){
            credits.setTotal_credits(credits.getTotal_credits()+creditsNum);
            creditsNum += credits.getUser_credits();
            credits.setUser_credits(creditsNum);
            creditsService.update(credits);
        }else{
            credits = new Credits();
            credits.setUser_id(creditsParam.getUser_id());
            credits.setCompany_id(creditsParam.getCompany_id());
            credits.setUser_credits(creditsNum);
            credits.setTotal_credits(creditsNum);
            creditsService.save(credits);
        }
        CreditsLog log =  JSON.parseObject(JSON.toJSONString(form),CreditsLog.class);
        log.setOpera_direction(1);
        creditsLogService.save(log);
        CreditsVO creditsVO= new CreditsVO();
        creditsVO.setUser_credits(credits.getUser_credits());
        creditsVO.setCost_credits(credits.getCost_credits());
        creditsVO.setHistory_credits(credits.getTotal_credits());
        return VOUtil.setSuccessResult(creditsVO);
    }

    public CreditsVO functionConsumeCredits(CreditsOperForm form){
        Credits creditsParam = JSON.parseObject(JSON.toJSONString(form),Credits.class);
        Credits credits = creditsService.getByUser(creditsParam);
        int creditsNum=form.getOpera_credits();
        if(credits !=null){
            if(credits.getUser_credits()<creditsNum ){
               return VOUtil.genErrorResult(HSBCPSErrorCodes.CreditsErrorInfo.CREDIT_NOT_ENOUGH,CreditsVO.class);
            }
            credits.setCost_credits(credits.getCost_credits()+creditsNum);
            credits.setUser_credits( credits.getUser_credits()-creditsNum);
            creditsService.update(credits);
        }else{
            return VOUtil.genErrorResult(HSBCPSErrorCodes.CreditsErrorInfo.CREDIT_NOT_ENOUGH,CreditsVO.class);
        }
        CreditsLog log =  JSON.parseObject(JSON.toJSONString(form),CreditsLog.class);
        log.setOpera_direction(-1);
        creditsLogService.save(log);
        CreditsVO creditsVO= new CreditsVO();
        creditsVO.setUser_credits(credits.getUser_credits());
        creditsVO.setCost_credits(credits.getCost_credits());
        creditsVO.setHistory_credits(credits.getTotal_credits());
        return VOUtil.setSuccessResult(creditsVO);
    }

    public CreditsVO functionGetCreditsByUser(UserCreditsForm form){
        Credits creditsParam = JSON.parseObject(JSON.toJSONString(form),Credits.class);
        Credits credits = creditsService.getByUser(creditsParam);
        CreditsVO creditsVO= new CreditsVO();
        creditsVO.setUser_id(form.getUser_id());
        if(credits!=null){
            creditsVO.setUser_credits(credits.getUser_credits());
            creditsVO.setCost_credits(credits.getCost_credits());
            creditsVO.setHistory_credits(credits.getTotal_credits());
        }
        return VOUtil.setSuccessResult(creditsVO);
    }

    public ResultVO functionQueryCredits(QueryCreditsForm form){
        Credits creditsParam = JSON.parseObject(JSON.toJSONString(form),Credits.class);
        Pagination pagination = new Pagination();
        pagination.setStart_index(form.getStart_row());
        pagination.setPage_size(form.getRequest_num());
        creditsParam.setPage(pagination);
        List<Credits> Creditss = creditsService.query(creditsParam);
        ResultVO result = new ResultVO();
        result.setData_str(JSON.toJSONString(Creditss));
        return result;
    }

    public ResultVO functionQueryCreditsLog(QueryCreditslogForm form){
        CreditsLog logParam =  JSON.parseObject(JSON.toJSONString(form),CreditsLog.class);
        Pagination pagination = new Pagination();
        pagination.setStart_index(form.getStart_row());
        pagination.setPage_size(form.getRequest_num());
        logParam.setPage(pagination);
        List<CreditsLog> CreditsLogs = creditsLogService.query(logParam);
        ResultVO result = new ResultVO();
        result.setData_str(JSON.toJSONString(CreditsLogs));
        return result;
    }



//    public CreditsLimitBackVO func_add_credlits_limit(CreditsLimitAddForm form){
//        CreditsLimit creditsLimit = JSON.parseObject(JSON.toJSONString(form),CreditsLimit.class);
//        creditsLimitService.save(creditsLimit);
//        CreditsLimitBackVO creditsLimitBackVO = new CreditsLimitBackVO();
//        creditsLimitBackVO.setCredits_limit_id(creditsLimit.getCredits_limit_id());
//        return VOUtil.setSuccessResult(creditsLimitBackVO);
//    }
//
//    public HSBCPSBackVO func_update_credlits_limit(CreditsLimitUpdateForm form){
//        CreditsLimit creditsLimit = JSON.parseObject(JSON.toJSONString(form),CreditsLimit.class);
//        creditsLimitService.update(creditsLimit);
//        return VOUtil.genSuccessBackVo();
//    }
//
//    public HSBCPSBackVO func_delete_credlits_limit(long  id){
//        creditsLimitService.delete(id);
//        return VOUtil.genSuccessBackVo();
//    }
//
//    public ResultVO func_query_credits_limit(CreditsLimitQueryForm form){
//        CreditsLimit creditsLimit = JSON.parseObject(JSON.toJSONString(form),CreditsLimit.class);
//        Pagination pagination = new Pagination();
//        pagination.setStart_index(form.getStart_row());
//        pagination.setPage_size(form.getRequest_num());
//        creditsLimit.setPage(pagination);
//        List<CreditsLimit> creditsLimits =creditsLimitService.query(creditsLimit);
//        return new ResultVO(JSON.toJSONString(creditsLimits));
//
//    }

}
